Skip to content

perf: cache packed proof nonces for cheaper header/block hash#3899

Open
iho wants to merge 1 commit into
mimblewimble:stagingfrom
iho:perf/proof-pack-nonces-cache
Open

perf: cache packed proof nonces for cheaper header/block hash#3899
iho wants to merge 1 commit into
mimblewimble:stagingfrom
iho:perf/proof-pack-nonces-cache

Conversation

@iho

@iho iho commented Jul 9, 2026

Copy link
Copy Markdown

Summary

block.hash() / header.hash() ultimately hash the PoW Proof by bit-packing all nonces on every call. That packing is relatively expensive and runs very often during sync and validation.

This caches the packed form when a Proof is deserialized (the on-wire bytes we already read), so later hash/write uses that buffer instead of re-running pack_bits.

Closes #3372

Approach

  • Add private packed_nonces: Option<Vec<u8>> on Proof (ignored by PartialEq / serde)
  • Readable::read stores the packed bytes alongside extracted nonces
  • Writeable::write prefers the cache; otherwise packs on demand (mining / constructed proofs)
  • Proof::clear_packed_cache() for safe mutation of edge_bits / nonces
  • Stratum share path clears cache after applying a solution
  • Unit tests: round-trip cache, hash equivalence, mutation + clear

Locally constructed proofs (mining templates, tests, genesis) keep None and behave as before.

Test plan

  • cargo test -p grin_core --lib pow::types
  • cargo test -p grin_core --lib genesis
  • cargo check -p grin_servers

block.hash() / header.hash() re-bitpacked Proof nonces on every call.
Keep the on-wire packed form when deserializing a Proof so subsequent
Hash serialization skips pack_bits. Locally built proofs pack on demand;
clear_packed_cache after mutating edge_bits/nonces (stratum share path).

Closes mimblewimble#3372
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant